package com.keur.money.ui.steps.impl

import android.content.Context
import com.keur.money.base.utils.log.MullaLog
import com.keur.money.http.ApiService
import com.keur.money.http.HttpHelper
import com.keur.money.http.IHttpCallback
import com.keur.money.mvp.presenter.MvpPresenter
import com.keur.money.ui.steps.view.IKeurUploadView
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/31 15:46
 * @Annotation：图片上传 Presenter 实现
 */
class KeurUploadPresenterImpl(context: Context?) : MvpPresenter<IKeurUploadView>(context) {

    /**
     * 上传图片
     */
    fun onMullaUploadImageAction(imgPath: String){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.KEUR_API_UPLOAD_FILE_URL)
        HttpHelper.uploadKeurFileAction(httpUrl,imgPath,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                mvpView?.hideMullaLoading()
                when(data.optInt("code")){
                    0 -> {//成功
                        if (data.has("obj")){
                            val obj = data.optJSONObject("obj")
                            obj?.let {
                                val imgUrl = it.optString("url")
                                mvpView?.showMullaUploadImageSuccess(imgPath,imgUrl)
                            }
                        }
                    }
                    else -> {
                        val msg = data.optString("msg")
                        mvpView?.showMullaToast(msg)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                MullaLog.e("upload error： $error")
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideMullaLoading()
            }
        })
//        HttpHelper.uploadMullaFileAction(httpUrl,imgPath,object : StringCallback(){
//            override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
//                super.onStart(request)
//                MullaLog.e("upload start")
//            }
//            override fun onSuccess(response: Response<String>?) {
//                if (response?.body() == null){
//                    return
//                }
//                MullaLog.e("upload success ："+ response.body())
//                if (response.code() == 200 && mvpView? != null){
//                    mvpView?.hideMullaLoading()
//                    val data = JSONObject(response.body())
//                    when(data.optInt("code")){
//                        0 -> {//成功
//                            if (data.has("obj") && data.optJSONObject("obj") != null){
//                                val imgUrl = data.optJSONObject("obj")?.optString("url")
//                                mvpView?.showMullaUploadImageSuccess(imgPath,imgUrl)
//                            }
//                        }
//                        else -> {
//                            val msg = data.optString("msg")
//                            mvpView?.showMullaToast(msg)
//                        }
//                    }
//                } else {
//                    if (mvpView? != null && response.message() != null){
//                        mvpView?.showMullaToast(response.message())
//                    }
//                }
//            }
//            override fun onError(response: Response<String>?) {
//                if (response?.body() == null){
//                    return
//                }
//                MullaLog.e("upload error： "+ response.body())
//                if (mvpView? != null && response.message() != null){
//                    mvpView?.showMullaToast(response.message())
//                }
//            }
//            override fun uploadProgress(progress: Progress) {
//               MullaLog.e("uploadProgress: $progress")
//               MullaLog.e("uploadProgress，total: ${progress.totalSize}")
//               MullaLog.e("uploadProgress，progress: ${progress.currentSize}")
//            }
//            override fun onFinish() {
//                if (mvpView? != null){
//                    mvpView?.hideMullaLoading()
//                }
//            }
//        })
    }
}